package com.itheima.wisdomnext.controller;

import com.itheima.wisdomnext.entity.Users;
import com.itheima.wisdomnext.entity.Result.ResponseResult;
import com.itheima.wisdomnext.service.ImpI.UserServiceImpI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import jakarta.validation.Valid;
import java.util.HashMap;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/api/user")
public class UserController {

    @Autowired
    private UserServiceImpI userService;

    // 注册
    @PostMapping("/register")
    public ResponseResult<Object> register(@Valid @RequestBody Users users) {
        String data = userService.registerUser(users);
        return ResponseResult.success(data, new HashMap<>());
    }

    // 登录
    @PostMapping("/login")
    public ResponseResult<Map<String, Object>> login(@Valid @RequestBody Users users) {
        String datatext = userService.loginUser(users);  // 这里会返回Token和userId字符串

        // 将token和userId分开处理
        String[] parts = datatext.split(", ");  // 根据逗号分隔
        String token = parts[0].split(": ")[1];  // 提取Token
        int userId = Integer.parseInt(parts[1].split(": ")[1]);  // 提取userId
        String avatar = (parts.length > 6 && parts[2].contains(": ")) ? parts[2].split(": ")[1] : "";

        Map<String, Object> data = new HashMap<>();
        data.put("userId", userId); // 添加 userId
        data.put("avatar", avatar);
        data.put("token", token); // 添加 token

        return ResponseResult.success("登录成功", data);
    }

    // 更新用户信息
    @PutMapping("/update")
    public ResponseResult<Object> updateUser(@Valid @RequestBody Users users) {
        userService.updateUserInfo(users);
        return ResponseResult.success("更新用户信息成功", new HashMap<>());
    }

    /**
     * 更新用户头像
     * @param userId 用户ID
     * @param avatar 上传的头像文件
     * @return 返回操作结果
     */
    @PutMapping("/update-avatar")
    public ResponseResult<Object> updateAvatar(@RequestParam("userId") int userId, @RequestParam("avatar") MultipartFile avatar) {
        // 调用服务层方法，更新头像
        String result = userService.updateAvatar(userId, avatar);
        // 返回操作结果
        return ResponseResult.success(result, new HashMap<>());
    }

    @GetMapping("/get/{userId}")
    public ResponseResult<Map<String, Object>> getUserById(@PathVariable("userId") int userId) {
        // 获取用户基本信息
        Users user = userService.getUserById(userId);
        // 获取点赞数和关注者数
        int likeCount = userService.getLikeCountByUserId(userId);
        int followerCount = userService.getFollowerCountByUserId(userId);
        int followedCount = userService.getFollowedCountByUserId(userId);

        // 创建一个 Map 来封装用户信息和附加的点赞数、关注者数
        Map<String, Object> result = new HashMap<>();
        result.put("user", user);
        result.put("likeCount", likeCount);
        result.put("followerCount", followerCount);
        result.put("followedCount", followedCount);

        return ResponseResult.success("查询成功", result);
    }
    // 查询所有用户信息
    @GetMapping("/all")
    public ResponseResult<Object> getAllUsers() {
        return ResponseResult.success("查询成功", userService.getAllUsers());
    }

}
